<?php

class MyFunction extends CActiveRecord {

    public static function generateRandomString($length = 10) {
        $randomstring = '';
        if ($length > 32) {
            $multiplier = round($length / 32, 0, PHP_ROUND_HALF_DOWN);
            $remainder = $length % 32;
            for ($i = 0; $i < $multiplier; $i++) {
                $randomstring .= substr(str_shuffle(md5(rand())), 0, 32);
            }
            $randomstring .= substr(str_shuffle(md5(rand())), 0, $remainder);
        }
        else
            $randomstring = substr(str_shuffle(md5(rand())), 0, $length);
        return $randomstring;
    }

    public static function convert_currency() {
        $condition = new EMongoCriteria;
        $condition->sort('timestamp', EMongoCriteria::SORT_DESC);
        $exchange = Exchange::model()->find($condition);
        $exchangeRates = $exchange->rates;
        $exchange_rates = array();
        foreach ($exchangeRates as $key => $value) {
            $exchange_rates['rates'][$key] = array(
                'id' => $key,
                'name' => $value['name'],
                'buy' => $value['buy'],
                'transfer' => $value['transfer'],
                'sell' => $value['sell']
            );
        }
        return $exchange_rates;
    }

    public static function getDate($dateString, $format = DEFAULT_DATE_FORMAT, $type = 'mongo') {

        $dateObj = new DateTime($dateString);
        if ($type == 'mongo') {
            return new MongoDate(strtotime($dateObj->format($format)));
        }
        if ($type == 'string') {
            return $dateObj->format($format);
        }
        return false;
    }

    public static function Convert_Hotel_to_Id($hotel) {
        $hotel = str_replace('-', '', $hotel);
        if (substr($hotel, 0, 17) == 'silverlandcentral') {
            $hotel = 1;
        } elseif (substr($hotel, 0, 18) == 'silverlandboutique') {
            $hotel = 3;
        } elseif (substr($hotel, 0, 13) == 'silverlandinn') {
            $hotel = 4;
        } elseif (substr($hotel, 0, 15) == 'grandsilverland') {
            $hotel = 5;
        } elseif (substr($hotel, 0, 7) == 'lajolie') {
            $hotel = 6;
        } elseif (substr($hotel, 0, 15) == 'silverlandsakyo') {
            $hotel = 7;
        } else {
            $hotel = 2;
        }
        return $hotel;
    }

    public static function Convert_Id_to_Name($hotel) {
        $hotel = str_replace('-', '', $hotel);
        if (substr($hotel, 0, 17) == 'silverlandcentral') {
            $hotel = 'Silverland Central - Tan Hai Long Hotel & Spa';
        } elseif (substr($hotel, 0, 18) == 'silverlandboutique') {
            $hotel = 'Silverland Boutique - Tan Hai Long 3 Hotel';
        } elseif (substr($hotel, 0, 13) == 'silverlandinn') {
            $hotel = 'Silverland Inn - Tan Hai Long 4 Hotel';
        } elseif (substr($hotel, 0, 15) == 'grandsilverland') {
            $hotel = 'Grand Silverland Hotel & Spa';
        } elseif (substr($hotel, 0, 7) == 'lajolie') {
            $hotel = 'La Jolie Hotel & Spa';
        } elseif (substr($hotel, 0, 15) == 'silverlandsakyo') {
            $hotel = 'Silverland Sakyo Hotel & Spa';
        } else {
            $hotel = 'Silverland Hotel & Spa';
        }
        return $hotel;
    }

    public static function country_zip() {
        return array(
            'PA7' => '93',
            'PA12' => '358',
            'PAL' => '355',
            'PDZ' => '213',
            'PAD' => '376',
            'PAO' => '244',
            'PA8' => '1264',
            'PAG' => '1268',
            'PAR' => '54',
            'PAM' => '374',
            'PA11' => '297',
            'PAU' => '61',
            'PAT' => '43',
            'PAZ' => '994',
            'PA2' => '1242',
            'PBH' => '973',
            'PBD' => '880',
            'PBB' => '1246',
            'PBY' => '375',
            'PBE' => '32',
            'PBZ' => '501',
            'PBJ' => '229',
            'PA13' => '1441',
            'PA14' => '975',
            'PBO' => '591',
            'PBA' => '387',
            'PBW' => '267',
            'PA27' => '47',
            'PBR' => '55',
            'PA32' => '246',
            'PA60' => '1284',
            'PBN' => '673',
            'PBG' => '359',
            'PBF' => '226',
            'PBI' => '257',
            'PKH' => '855',
            'PCM' => '237',
            'PCA' => '1',
            'PA17' => '238',
            'PA36' => '1345',
            'PCF' => '236',
            'PTD' => '235',
            'PCL' => '56',
            'PCN' => '86',
            'PA18' => '618',
            'PA15' => '61',
            'PCO' => '57',
            'PA35' => '269',
            'PCG' => '242',
            'PA16' => '682',
            'PCR' => '506',
            'PHR' => '385',
            'PCU' => '53',
            'PCY' => '357',
            'PCZ' => '420',
            'PCD' => '243',
            'PDK' => '45',
            'PDJ' => '253',
            'PDM' => '1767',
            'PDO' => '1',
            'PEC' => '593',
            'PEG' => '20',
            'PSV' => '503',
            'PGQ' => '240',
            'PER' => '291',
            'PEE' => '372',
            'PET' => '251',
            'PA20' => '500',
            'PA22' => '298',
            'PA1' => '679',
            'PFI' => '358',
            'PFR' => '33',
            'PGF' => '594',
            'PPF"' => '689',
            'PGA' => '241',
            'PGM' => '220',
            'PGE' => '995',
            'PDE' => '49',
            'PGH' => '233',
            'PGI' => '350',
            'PGR' => '30',
            'PA28' => '299',
            'PA23' => '1473',
            'PGP' => '590',
            'PA26' => '1671',
            'PGT' => '502',
            'PA24' => '44',
            'PGN' => '224',
            'PA29' => '245',
            'PGY' => '594',
            'PHT' => '509',
            'PA30' => '61',
            'PHN' => '504',
            'PA69' => '852',
            'PHU' => '36',
            'PIS' => '354',
            'PIN' => '91',
            'PID' => '62',
            'PIR' => '98',
            'PIQ' => '964',
            'PIE' => '353',
            'PA31' => '44',
            'PIL' => '972',
            'PIT' => '39',
            'PCI' => '225',
            'PJM' => '1876',
            'PJP' => '81',
            'PA33' => '44',
            'PJO' => '962',
            'PKZ' => '7',
            'PKE' => '254',
            'PA34' => '686',
            'PA71' => '381',
            'PKW' => '965',
            'PKG' => '996',
            'PLA' => '856',
            'PLV' => '371',
            'PLB' => '961',
            'PA40' => '266',
            'PA39' => '231',
            'PLY' => '218',
            'PA38' => '423',
            'PLT' => '370', 'PLU' => '352', 'PA70' => '853', 'PMK' => '389', 'PMG' => '261', 'PMW' => '265', 'PMY' => '60',
            'PA3' => '960', 'PML' => '223', 'PMT' => '356',
            'PA41' => '692', 'PMQ' => '596', 'PMR' => '222', 'PMU' => '230', 'PYT' => '262', 'PMX' => '52',
            'PA21' => '691', 'PMD' => '373', 'PMC' => '377', 'PMN' => '976', 'PA68' => '382', 'PA43' => '1664', 'PMA' => '212',
            'PMZ' => '258', 'PMM' => '95', 'PNA' => '264', 'PA44' => '674',
            'PNP' => '977', 'PNL' => '31', 'PA9' => '599', 'PNC' => '687', 'PNZ' => '64', 'PNI' => '505',
            'PNE' => '227', 'PNG' => '234',
            'PA45' => '683', 'PA6' => '672', 'PKP' => '850', 'PA42' => '1670', 'PNO' => '47', 'POM' => '968', 'PPK' => '926',
            'PA48' => '80', 'PA47' => '970', 'PPA' => '507', 'PPG' => '675', 'PPY' => '595', 'PPE' => '51',
            'PPH' => '63', 'PA46' => '672', 'PPL' => '48', 'PPT' => '351', 'PPR' => '1', 'PQA' => '974',
            'PRE' => '262', 'PRO' => '40', 'PRU' => '7', 'PRW' => '250', 'PSF' => '590', 'PA66' => '590', 'PKN' => '1869',
            'PA37' => '1758', 'PA65' => '590', 'PA10' => '684', 'PA64' => '685', 'PSM' => '378', 'PA52' => '239',
            'PSA' => '966', 'PSN' => '221', 'PA67' => '381', 'PA50' => '248', 'PSL' => '232', 'PSG' => '65', 'PSK' => '421', 'PSI' => '386',
            'PA49' => '677', 'PSO' => '252', 'PZA' => '27', 'PA25' => '54', 'PKR' => '82', 'PES' => '34', 'PLK' => '94', 'PPM' => '508',
            'PA59' => '1784', 'PSD' => '249', 'PSR' => '597', 'PA51' => '47', 'PA53' => '268', 'PSE' => '46', 'PCH' => '41', 'PSY' => '963',
            'PTW' => '886', 'PTJ' => '992', 'PTZ' => '255', 'PTH' => '66', 'PA55' => '670', 'PTG' => '228', 'PA54' => '690', 'PA56' => '676',
            'PTT' => '1868', 'PTN' => '216', 'PTR' => '90', 'PTM' => '993', 'PA4' => '1649', 'PA57' => '688', 'PUG' => '256',
            'PUA' => '380', 'PAE' => '971', 'PGB' => '44', 'PUS' => '1', 'PA58' => '1', 'PUY' => '598', 'PUZ' => '998', 'PA62' => '678',
            'PVA' => '379', 'PVE' => '58', 'PVN' => '84', 'PA61' => '1340', 'PA63' => '681', 'PA19' => '212', 'PYE' => '967', 'PZM' => '260',
            'PZW' => '263'
        );
    }

}

/* "file" : "?<?xml version="1.0" encoding="utf-8" standalone="yes"?><!--For reference only. Only one request every5 minutes!--><ExrateList>  <DateTime>4/14/2014 9:54:26 AM</DateTime>  <Exrate CurrencyCode="AUD" CurrencyName="AUST.DOLLAR" Buy="19619.64" Transfer="19738.07" Sell="19945.89" />  <Exrate CurrencyCode="CAD" CurrencyName="CANADIAN DOLLAR" Buy="18926.43" Transfer="19098.31" Sell="19338.04" />  <Exrate CurrencyCode="CHF" CurrencyName="SWISS FRANCE" Buy="23739.84" Transfer="23907.19" Sell="24207.28" />  <Exrate CurrencyCode="DKK" CurrencyName="DANISH KRONE" Buy="0" Transfer="3873.49" Sell="3953.61" />  <Exrate CurrencyCode="EUR" CurrencyName="EURO" Buy="28997.16" Transfer="29084.41" Sell="29331.92" />  <Exrate CurrencyCode="GBP" CurrencyName="BRITISH POUND" Buy="34888.09" Transfer="35134.03" Sell="35433.03" />  <Exrate CurrencyCode="HKD" CurrencyName="HONGKONG DOLLAR" Buy="2679.65" Transfer="2698.54" Sell="2743.37" />  <Exrate CurrencyCode="INR" CurrencyName="INDIAN RUPEE" Buy="0" Transfer="343.61" Sell="357.8"/>  <Exrate CurrencyCode="JPY" CurrencyName="JAPANESE YEN" Buy="204.7" Transfer="206.77" Sell="208.95" />  <Exrate CurrencyCode="KRW" CurrencyName="SOUTH KOREAN WON" Buy="0" Transfer="18.45" Sell="22.56"},"KWD" CurrencyName="KUWAITI DINAR" Buy="0" Transfer="74178.29" Sell="75712.72" />  <Exrate CurrencyCode="MYR" CurrencyName="MALAYSIAN RINGGIT" Buy="0" Transfer="6469.44" Sell="6550.64" />  <Exrate CurrencyCode="NOK" CurrencyName="NORWEGIAN KRONER" Buy="0" Transfer="3512.39" Sell="3585.05" />  <Exrate CurrencyCode="RUB" CurrencyName="RUSSIAN RUBLE" Buy="0" Transfer="537.99" Sell="657.86" />  <Exrate CurrencyCode="SAR" CurrencyName="SAUDI RIAL" Buy="0" Transfer="5459.9" Sell="5800.38" />  <Exrate CurrencyCode="SEK" CurrencyName="SWEDISH KRONA" Buy="0" Transfer="3197.42" Sell="3250.53" />  <Exrate CurrencyCode="SGD" CurrencyName="SINGAPORE DOLLAR" Buy="16632.91" Transfer="16750.16" Sell="17028.39" />  <Exrate CurrencyCode="THB" CurrencyName="THAI BAHT" Buy="640.93" Transfer="640.93" Sell="667.41" />  <Exrate CurrencyCode="USD" CurrencyName="US DOLLAR" Buy="21080" Transfer="21080" Sell="21120" />  <Source>Joint Stock Commercial Bank for Foreign Trade of Vietnam - Vietcombank</Source></ExrateList>", "timestamp" : 1397444345 }>
  "AUD":{"name":"AUST.DOLLAR","buy":"19619.64","transfer":"19738.07","sell":"19945.89"},
  "CAD":{"name":"CANADIAN DOLLAR","buy":"18926.43","transfer":"19098.31","sell":"19338.04"},
  "CHF":{"name":"SWISS FRANCE","buy":"23739.84","transfer":"23907.19","sell":"24207.28"},
  "DKK":{"name":"DANISH KRONE","buy":"0","transfer":"3873.49","sell":"3953.61"},
  "EUR" :{"name":"EURO", "buy":"28997.16", "transfer":"29084.41","sell":"29331.92"},
  "GBP":{"name":"BRITISH POUND","buy":"34888.09","transfer":"35134.03","sell":"35433.03"},
  "HKD":{"name":"HONGKONG DOLLAR","buy":"2679.65", "transfer":"2698.54","sell":"2743.37"},
  "INR":{"name":"INDIAN RUPEE","buy":"0","transfer":"343.61","sell":"357.8"},
  "JPY":{"name":"JAPANESE YEN","buy":"204.7","transfer":"206.77","sell":"208.95"},
  "KRW":{"name":"SOUTH KOREAN WON","buy":"0","transfer":"18.45","sell":"22.56"},
  "KWD":{"name":"KUWAITI DINAR","buy":"0","transfer":"74178.29","sell":"75712.72"},
  "MYR":{"name":"MALAYSIAN RINGGIT","buy":"0","transfer":"6469.44","sell":"6550.64"},
  "NOK":{"name":"NORWEGIAN KRONER","buy":"0","transfer":"3512.39","sell":"3585.05"},
  "RUB":{"name":"RUSSIAN RUBLE","buy":"0","transfer":"537.99","sell":"657.86"},
  "SAR":{"name":"SAUDI RIAL","buy":"0","transfer":"5459.9","sell":"5800.38"},
  "SEK":{"name":"SWEDISH KRONA","buy":"0","transfer":"3197.42","sell":"3250.53"}
  "SGD":{"name":"SINGAPORE DOLLAR","buy":"16632.91","transfer":"16750.16","sell":"17028.39"},
  "THB":{"name":"THAI BAHT","buy":"640.93","transfer":"640.93","sell":"667.41"},
  "USD":{"name":"US DOLLAR","buy":"21080","transfer":"21080","sell":"21120"},
  "VND":{"name":"VIET NAM DONG","buy":"1","transfer":"1","sell":"1"},
 */
?>